package com.tedsoft.first_group_project;

import java.util.List;
import com.tedsoft.first_group_project.Settings.GameState;
import com.tedsoft.framework.*;
import com.tedsoft.framework.Input.TouchEvent;
//import android.util.Log;

public class MainGameScreen extends Screen {
	
	GameEngine gameEngine;
	PauseMenu PauseMenu;
	
		
    public MainGameScreen(Game game) {
    	
        super(game);               
    }   

    @Override
    public void update(float deltaTime) {
    	
    	//utworzenie obiektu silnika gry, jezeli nie istnieje
    	if(gameEngine == null)
    	gameEngine = new GameEngine(game);
    	if (PauseMenu == null)
    	PauseMenu = new PauseMenu();
    	
    	List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        
        if(Settings.state == GameState.Running)
            updateRunning(touchEvents, deltaTime);
        else if(Settings.state == GameState.Paused)
            PauseMenu.update(game, touchEvents);
        else if(Settings.state == GameState.GameOver)
        	updateGameOver(touchEvents);
    }
    
    public void updateRunning(List<TouchEvent> touchEvents, float deltaTime){         
    	//pobranie obiektu obslugi grafiki
    	Graphics g = game.getGraphics();
    	
    	//MUZA
    	if (Settings.soundEnabled)
    	{
        	Assets.muza.play();
        }
  
    	//UPDATE ACCELEROMETRU
    	gameEngine.accel_meter.update(game.getInput().getAccelY()); 
    	
    	//UPDATE GLOWNEGO SILNIKA
        gameEngine.update(deltaTime, game);
       
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) 
        {
			TouchEvent event = touchEvents.get(i);
			if(event.type == TouchEvent.TOUCH_DOWN)
			{
				//Log.v("INPUT_event", "TOUCH_DOWN" );
				
				//if(gameEngine.player.touching_ground)
				//	gameEngine.player.velocityY = 0;
				
				//dotkniecie ekranu
				//if(gameEngine.player.touching_ground)
				
					gameEngine.player.touching_ground = false;
					//gameEngine.player.velocityY = -40.0f;
					gameEngine.player.falling_down = false;
					
					if(event.x > g.getWidth()-80 && event.x < g.getWidth()-40 && event.y < 40)  //guzik mute
					{
	                    if (Settings.soundEnabled){
	                    	Assets.muza.pause();
	                    	Settings.soundEnabled = !Settings.soundEnabled;}
	                    else{
	                    	Assets.muza.play();
	                    	Settings.soundEnabled = !Settings.soundEnabled;}
	                    	
	                    
					}
					if(event.x > g.getWidth()-40 && event.y < 40){  //guzik menu
					Settings.state = GameState.Paused;
					gameEngine.player.falling_down = true;
					}
			}
			

			
			if(event.type == TouchEvent.TOUCH_DRAGGED)
			{
				//Log.v("INPUT_event", "TOUCH_DRAGGED" );
			}
			
			if(event.type == TouchEvent.TOUCH_UP)
			{
				//Log.v("INPUT_event", "TOUCH_UP" );
				gameEngine.player.falling_down = true;
			}		
        }	
        
        if(gameEngine.gameOver == true)
		{
			Settings.state = GameState.GameOver;
		}
        
    }      
    
    /*
    private boolean inBounds(TouchEvent event, int x, int y, int width, int height) {
        if(event.x > x && event.x < x + width - 1 && 
           event.y > y && event.y < y + height - 1) 
            return true;
        else
            return false;
    }
    */

     
    private void updateGameOver(List<TouchEvent> touchEvents) {
    	Graphics g = game.getGraphics();
    	int len = touchEvents.size();
    	if (Settings.soundEnabled)
        	Assets.muza.pause();
    	for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_DOWN) {
        	
                if(event.x > (50) && event.x <= (50+240)) {
                    if(event.y > 230 && event.y <= (230+50)) {                 
                    	if (Settings.soundEnabled)
                        	Assets.muza.play();
                    	
                    	gameEngine.gameOver = false;
                    	Settings.state = GameState.Running;
                    	gameEngine.gameReset(game);
                    	
                        return;
                    }                    

                }
                
            }
        }	
    }    
    
    @Override
    public void present(float deltaTime) {
    	Graphics g = game.getGraphics();
    	//tutaj rysowanie elementow
        gameEngine.present(deltaTime, game);
        if(Settings.state == GameState.Running)
            drawRunning();
        if(Settings.state == GameState.Paused)
            PauseMenu.rysuj(g);
        if(Settings.state == GameState.GameOver)
        	drawGameOver();
        }
    public void drawRunning(){	
        Graphics g = game.getGraphics();
        //rysowanie ikonki od mute
        if(Settings.soundEnabled)
            g.drawPixmap(Assets.buttons, g.getWidth()-80, 0, 0, 0, 40, 40);
        else
        	g.drawPixmap(Assets.buttons, g.getWidth()-80, 0, 40, 0, 40, 40);
        //ikonka menu
        g.drawPixmap(Assets.buttons, g.getWidth()-40, 0, 81, 0, 40, 40);
        //rysowanie aktualnego stanu accelerometru
        gameEngine.accel_meter.rysuj(g);
    }
  
    public void drawGameOver(){
    	Graphics g = game.getGraphics();
		
    	g.drawText("GAME OVER", 100, 150,  Color.convert(255, 255, 255, 255), 40);
    	g.drawText("score: "+String.valueOf(gameEngine.player.getTotalScore(gameEngine.gameDiff.currentDiff)), 100, 180,  Color.convert(255, 255, 255, 255), 30);
    	g.drawText("length: "+String.valueOf((int)gameEngine.player.posX)+"px", 100, 220,  Color.convert(255, 255, 255, 255), 30);
    	
    	//g.drawText("click here to restart", 50, 270,  Color.convert(0, 255, 255, 255), 40);

    	g.drawPixmap(Assets.click_to_restart_button, 50, 230);
   	
    }  
    
    @Override
    public void pause() { 
    	Assets.muza.stop();
        //Settings.save(game.getFileIO());
    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
    	Assets.muza.stop();
    }
}
